﻿using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using Common;
using CommonModel.Constant;
using IBll;

namespace Bll
{
    /// <summary>
    /// 文件服务
    /// </summary>
    public class FileBll:IFileBll
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="customFolderName"></param>
        /// <returns></returns>
        public bool UploadFile(string customFolderName)
        {
            bool result = false;

            HttpFileCollection httpFileCollection = HttpContext.Current.Request.Files;
            if (httpFileCollection.Count > 0)
            {
                LogHelper.Debug("需要上传的文件数：" + httpFileCollection.Count);
                for (int i = 0; i < httpFileCollection.Count; i++)
                {
                    string fileCustomRelativePath = GetFileCustomRelativePath(customFolderName, httpFileCollection[i]);
                    string filePath = RelativePath.UploadFileFolder + fileCustomRelativePath; // 文件路径
                    string fileAbsolutePath = AbsolutePath.UploadFileFolder + fileCustomRelativePath; //文件绝对路径
                    string fileDirectory = GetFileDirectory(fileAbsolutePath); //文件绝对路径目录
                    if (!Directory.Exists(fileDirectory))
                    {
                        Directory.CreateDirectory(fileDirectory);
                    }
                    httpFileCollection[i].SaveAs(fileAbsolutePath);
                    LogHelper.Debug("上传文件成功：" + filePath);
                    #region FilePath根据customFolderName，入不同表字段保存

                    #endregion
                }
                result = true;
            }
            else
            {
                LogHelper.Debug("没有文件可上传");
            }
            LogHelper.Debug("上传文件结束");

            return result;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public void DownloadFile(string filePath)
        {
            string fileAbsolutePath = HttpContext.Current.Server.MapPath(filePath);
            LogHelper.Debug("下载文件："+ filePath);
            if (File.Exists(fileAbsolutePath))
            {
                FileDownHelper.DownLoad(filePath);
                LogHelper.Debug("下载文件结束：" + filePath);
            }
        }

        #region 私有方法
        /// <summary>
        /// 获取文件目录
        /// </summary>
        /// <param name="fileAbsolutePath"></param>
        /// <returns></returns>
        private string GetFileDirectory(string fileAbsolutePath)
        {
            return fileAbsolutePath.Substring(0, fileAbsolutePath.LastIndexOf('/'));
        }
        /// <summary>
        /// 获取文件自定义相对路径
        /// </summary>
        /// <param name="customFolderName"></param>
        /// <param name="httpPostFile"></param>
        /// <returns></returns>
        private string GetFileCustomRelativePath(string customFolderName,HttpPostedFile httpPostFile)
        {
            string fileExtension = Path.GetExtension(httpPostFile.FileName);
            string fileName = StringHelper.GetGuidString() + fileExtension;//文件名
            return "/"+ customFolderName+StringHelper.GetYearMonthDayFolderPath() + "/" + fileName;
        }
        #endregion
    }
}
